from django.utils import simplejson
from django.core.serializers import json
from django.http import HttpResponse
import settings
from megafeed import *


class MegaMiddleware(object):
    def process_response(self, request, response):
        if isinstance(response, HttpResponse):
            return response
        if settings.SHOW_QUERIES:
            from django.db import connection
            response['queries'] = connection.queries

        if request.path.endswith('json'):
            if settings.JSONP_PREFIX in request.REQUEST:
                return HttpResponse(request.REQUEST[settings.JSONP_PREFIX] + '('
                    + simplejson.dumps(response, cls=json.DjangoJSONEncoder,
                    ensure_ascii=False) + ')', mimetype='application/javascript')
            else:
                return HttpResponse(simplejson.dumps(response,
                       cls=json.DjangoJSONEncoder, ensure_ascii=False),
                       mimetype='application/json')
        else:
            return HttpResponse(xml_serialize(response), mimetype='application/xml')
